<template>
  <div>
    <h2>库存统计</h2>
    <div id="chart3" class="chart"></div>
  </div>
</template>

<script>
import { inject, onMounted, reactive } from "vue";
export default {
  name: "ItemThree",
  setup() {
    let $echarts = inject("echarts");
    let $axios = inject("axios");
    let data = reactive({});

    async function getData() {
      data = await $axios.get(`/three/data`);
    }

    onMounted(() => {
      getData().then(() => {
        let myEchart = $echarts.init(document.getElementById("chart3"));
        myEchart.setOption({
          legend: {
            show: true,
            top: "83%",
            textStyle: {
              color: "#fff",
              fontSize: 12
            }
          },
          grid: {
            left: "2%",
            right: "6%",
            bottom: "20%",
            containLabel: true
          },
          tooltip: {
            show: true
          },
          series: [
            {
              type: "pie",
              data: data.data.data.chartData,
              radius: [10, 100],
              center: ["50%", "45%"],
              roseType: "area",
              itemStyle: {
                borderRadius: 10
              }
            }
          ]
        });
        // 让echarts图表自适应
        window.addEventListener("resize", () => {
          myEchart.resize();
        });
      });
    });

    return { getData, data };
  }
};
</script>

<style scoped lang="less">
h2 {
  height: 0.6rem;
  line-height: 0.6rem;
  color: #fff;
  font-size: 0.25rem;
  text-align: center;
}
.chart {
  height: 4.5rem;
}
</style>
